import request from '@/utils/request'

export function getMenusTree(pid) {
  return request({
    url: 'api/menu/childNodes/' + pid,
    method: 'post',
    loading: true,
  })
}

export function getChild(id) {
  return request({
    url: '/api/menu/childMenus/' + id,
    method: 'post',
  })
}

export async function buildMenus() {
  return request({
    url: 'api/dynamicMenu/menus',
    method: 'post',
  })
}

export function addMenu(data) {
  return request({
    url: '/api/menu/create',
    method: 'post',
    data,
  })
}

export function delMenu(data) {
  return request({
    url: '/api/menu/delete',
    method: 'post',
    data: { menuId: data.menuId, pid: data.pid },
  })
}

export function editMenu(data) {
  return request({
    url: '/api/menu/update',
    method: 'post',
    data,
  })
}

export function treeByMenuId(menuId) {
  return request({
    url: '/api/menu/nodesByMenuId/' + menuId,
    method: 'post',
  })
}

export function queryCondition(data) {
  return request({
    url: '/api/menu/search',
    method: 'post',
    data,
  })
}

export function queryMenuTree(data) {
  return request({
    url: '/api/menu/tree',
    method: 'post',
    data,
  })
}

// 获取菜单树结构
export async function requestMenuTree(data) {
  let ret = { success: false, data: [] }
  try {
    const rsp = await queryMenuTree(data)
    if (rsp.success) {
      ret = { success: true, data: { listObject: rsp.data, total: undefined } }
    }
  } catch (error) {
    console.log(error)
  }

  return ret
}

export default { addMenu, editMenu, delMenu, getMenusTree, getChild, treeByMenuId, queryCondition }
